package com.wd.jetpack.model.data;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;

public class DatabaseContext extends ContextWrapper {
    private Context mbase;
    private File mdatabaseDirectory;

    public DatabaseContext(Context base, File databaseDirectory) {
        super(base);
        this.mbase = base;
        this.mdatabaseDirectory = databaseDirectory;
    }

    @Override
    public SQLiteDatabase openOrCreateDatabase(
            String name,
            int mode,
            SQLiteDatabase.CursorFactory factory
    ) {
        return openOrCreateDatabase(name, mode, factory, null);
    }

    @Override
    public SQLiteDatabase openOrCreateDatabase(
            String name,
            int mode,
            SQLiteDatabase.CursorFactory factory,
            DatabaseErrorHandler errorHandler
    ) {
        return mbase.openOrCreateDatabase(getDatabasePath(name).getPath(),
                mode,
                factory,
                errorHandler);
    }

    @Override
    public File getDatabasePath(String name) {
        return new File(mdatabaseDirectory.getPath(), name);
    }
}